package com.mxdx.edu.conf;

import java.util.List;

import com.mxdx.edu.model.StudentInfo;

import io.rong.RongCloud;
import io.rong.methods.group.Group;
import io.rong.methods.user.User;
import io.rong.models.Result;
import io.rong.models.group.GroupModel;
import io.rong.models.response.GroupUserQueryResult;
import io.rong.models.response.TokenResult;
import io.rong.models.response.UserResult;
import io.rong.models.user.UserModel;
import io.rong.util.HostType;

/**
 * 融云配置信息
 * 
 * @author hjg
 *
 */
public class RongcloudConfig {

	private final static String APP_KEY = "x18ywvqfx5hec";

	private final static String APP_SECRET = "4x5SBrXYle";

	@SuppressWarnings("static-access")
	public static void config(RongCloud rongCloud) {
		// 设置连接超时时间
		rongCloud.getApiHostType().setConnectTimeout(10000);
		// 设置读取超时时间
		rongCloud.getApiHostType().setReadTimeout(10000);
		// 获取备用域名List
		List<HostType> hosttypes = rongCloud.getApiHostListBackUp();
		// 设置连接、读取超时时间
		for (HostType hosttype : hosttypes) {
			hosttype.setConnectTimeout(10000);
			hosttype.setReadTimeout(10000);
		}
	}

	/**
	 * 注册融云用户
	 * 
	 * @param student
	 * @return
	 */
	public static String register(StudentInfo student) {
		RongCloud rongCloud = RongCloud.getInstance(APP_KEY, APP_SECRET);
		RongcloudConfig.config(rongCloud);
		User user = rongCloud.user;
		UserModel userModel = new UserModel(student.getId(), student.getStudentName(), student.getPersonProfile());
		String token = null;
		try {
			TokenResult json = user.register(userModel);
			token = json.getToken();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return token;
	}

	/**
	 * 更新用户信息
	 * 
	 * @param user
	 * @return
	 * @throws Exception
	 */
	public static Result update(StudentInfo student) throws Exception {
		RongCloud rongCloud = RongCloud.getInstance(APP_KEY, APP_SECRET);
		RongcloudConfig.config(rongCloud);
		User user = rongCloud.user;
		UserModel userModel = new UserModel(student.getId(), student.getStudentName(), student.getPersonProfile());
		Result result = user.update(userModel);
		return result;
	}

	/**
	 * 查询用户信息方法
	 */
	public static UserResult get(StudentInfo student) throws Exception {
		RongCloud rongCloud = RongCloud.getInstance(APP_KEY, APP_SECRET);
		RongcloudConfig.config(rongCloud);
		User user = rongCloud.user;
		UserModel userModel = new UserModel(student.getId(), student.getStudentName(), student.getPersonProfile());
		UserResult result = user.get(userModel);
		return result;
	}

	/**
	 * 创建群组
	 * 
	 * @param group
	 * @return
	 * @throws Exception
	 */
	public static Result createGroup(GroupModel groupModel) throws Exception {
		RongCloud rongCloud = RongCloud.getInstance(APP_KEY, APP_SECRET);
		RongcloudConfig.config(rongCloud);
		Group group = rongCloud.group;
		return (Result) group.create(groupModel);
	}

	/**
	 * 解散群组
	 * 
	 * @param groupModel
	 * @return
	 * @throws Exception
	 */
	public static Result dismissGroup(GroupModel groupModel) throws Exception {
		RongCloud rongCloud = RongCloud.getInstance(APP_KEY, APP_SECRET);
		RongcloudConfig.config(rongCloud);
		Group group = rongCloud.group;
		return (Result) group.dismiss(groupModel);
	}

	/**
	 * 加入群组
	 * 
	 * @param groupModel
	 * @return
	 * @throws Exception
	 */
	public static Result joinGroup(GroupModel groupModel) throws Exception {
		RongCloud rongCloud = RongCloud.getInstance(APP_KEY, APP_SECRET);
		RongcloudConfig.config(rongCloud);
		Group group = rongCloud.group;
		return (Result) group.join(groupModel);
	}

	/**
	 * 退出群组
	 * 
	 * @param groupModel
	 * @return
	 * @throws Exception
	 */
	public static Result quitGroup(GroupModel groupModel) throws Exception {
		RongCloud rongCloud = RongCloud.getInstance(APP_KEY, APP_SECRET);
		RongcloudConfig.config(rongCloud);
		Group group = rongCloud.group;
		return (Result) group.quit(groupModel);
	}

	/**
	 * 查询群组
	 * 
	 * @param groupModel
	 * @return
	 * @throws Exception
	 */
	public static GroupUserQueryResult queryGroup(GroupModel groupModel) throws Exception {
		RongCloud rongCloud = RongCloud.getInstance(APP_KEY, APP_SECRET);
		RongcloudConfig.config(rongCloud);
		Group group = rongCloud.group;
		return (GroupUserQueryResult) group.get(groupModel);
	}

	/**
	 * 刷新群组
	 * 
	 * @param groupModel
	 * @return
	 * @throws Exception
	 */
	public static Result updateGroup(GroupModel groupModel) throws Exception {
		RongCloud rongCloud = RongCloud.getInstance(APP_KEY, APP_SECRET);
		RongcloudConfig.config(rongCloud);
		Group group = rongCloud.group;
		return (Result) group.update(groupModel);
	}

}
